/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/* Written by Victor Ananjevsky, 2010-2014 */

#include "config.h"

#include <glib.h>
#include <glib/gi18n.h>

#include <tag_c.h>

enum {
  MTAG_MODE_INFO,
  MTAG_MODE_SHELL,
  MTAG_MODE_SET
};

static gint mode = MTAG_MODE_INFO;

static gboolean is_set = FALSE;
static gboolean is_shell = FALSE;
static gboolean is_utf = FALSE;
static gchar *mtag_title = NULL;
static gchar *mtag_artist = NULL;
static gchar *mtag_album = NULL;
static gint mtag_year = -1;
static gchar *mtag_comment = NULL;
static gint mtag_track = -1;
static gchar *mtag_genre = NULL;

static gchar **filenames = NULL;

static GOptionEntry entries[] = {
  { "set", 's', 0, G_OPTION_ARG_NONE, &is_set, N_("set tags"), NULL },
  { "shell", 'S', 0, G_OPTION_ARG_NONE, &is_shell, N_("show tags in shell-mode"), NULL },
  { "utf", 'u', 0, G_OPTION_ARG_NONE, &is_utf, N_("use UTF8 in tags"), NULL },
  { "title", 't', 0, G_OPTION_ARG_STRING, &mtag_title, N_("set title tag"), N_("STRING") },
  { "artist", 'a', 0, G_OPTION_ARG_STRING, &mtag_artist, N_("set artist tag"), N_("STRING") },
  { "album", 'A', 0, G_OPTION_ARG_STRING, &mtag_album, N_("set album tag"), N_("STRING") },
  { "year", 'y', 0, G_OPTION_ARG_INT, &mtag_year, N_("set year tag"), N_("NUM") },
  { "comment", 'c', 0, G_OPTION_ARG_STRING, &mtag_comment, N_("set comment tag"), N_("STRING") },
  { "track", 'T', 0, G_OPTION_ARG_INT, &mtag_track, N_("set track tag"), N_("NUM") },
  { "genre", 'g', 0, G_OPTION_ARG_STRING, &mtag_genre, N_("set genre tag"), N_("STRING") },
  { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, NULL },
  { NULL }
};

int
main (int argc, char *argv[])
{
  gint sec, min;
  GError *err = NULL;
  GOptionContext *cnt;
  TagLib_File *file;
  TagLib_Tag *tag;
  const TagLib_AudioProperties *props;

  setlocale (LC_ALL, "");

#ifdef ENABLE_NLS
  bindtextdomain (PACKAGE, LOCALEDIR);
  bind_textdomain_codeset (PACKAGE, "UTF-8");
  textdomain (PACKAGE);
#endif

  taglib_set_strings_unicode (0);
  /* XXX: hack, must be customizable */
  taglib_id3v2_set_default_text_encoding (TagLib_ID3v2_UTF8);

  cnt = g_option_context_new ("FILENAME");
  g_option_context_add_main_entries (cnt, entries, GETTEXT_PACKAGE);
  if (!g_option_context_parse (cnt, &argc, &argv, &err))
    {
      g_printerr (_("Option parsing failed: %s\n"), err->message);
      return 1;
    }

  if (is_set)
    mode = MTAG_MODE_SET;
  else if (is_shell)
    mode = MTAG_MODE_SHELL;

  if (is_utf)
    taglib_set_strings_unicode (1);

  if (!filenames || !filenames[0])
    return 1;

  file = taglib_file_new (filenames[0]);
  if (file == NULL)
    {
      g_printerr (_("Cannot open file '%s'\n"), filenames[0]);
      return 1;
    }

  tag = taglib_file_tag (file);

  switch (mode)
    {
    case MTAG_MODE_INFO:
      g_printf (_("\n*** TAGS ***\n"));
      g_printf (_("Title:   %s\n"), taglib_tag_title (tag));
      g_printf (_("Artist:  %s\n"), taglib_tag_artist (tag));
      g_printf (_("Album:   %s\n"), taglib_tag_album (tag));
      g_printf (_("Year:    %i\n"), taglib_tag_year (tag));
      g_printf (_("Comment: %s\n"), taglib_tag_comment (tag));
      g_printf (_("Track:   %i\n"), taglib_tag_track (tag));
      g_printf (_("Genre:   %s\n"), taglib_tag_genre (tag));

      props = taglib_file_audioproperties (file);

      min = taglib_audioproperties_length (props) / 60;
      sec = taglib_audioproperties_length (props) % 60;

      g_printf (_("\n*** AUDIO ***\n"));
      g_printf (_("Bitrate:     %i\n"), taglib_audioproperties_bitrate (props));
      g_printf (_("Sample Rate: %i\n"), taglib_audioproperties_samplerate (props));
      g_printf (_("Channels:    %i\n"), taglib_audioproperties_channels (props));
      g_printf (_("Length:      %i:%02i\n"), min, sec);
      break;

      /* FIXME: escape shell special chars here */
    case MTAG_MODE_SHELL:
      g_printf ("MTAG_TITLE='%s'\n", g_shell_quote (taglib_tag_title (tag)));
      g_printf ("MTAG_ARTIST='%s'\n", g_shell_quote (taglib_tag_artist (tag)));
      g_printf ("MTAG_ALBUM='%s'\n", g_shell_quote (taglib_tag_album (tag)));
      g_printf ("MTAG_YEAR=%i\n",taglib_tag_year (tag));
      g_printf ("MTAG_COMMENT='%s'\n", g_shell_quote (taglib_tag_comment (tag)));
      g_printf ("MTAG_TRACK=%i\n", taglib_tag_track (tag));
      g_printf ("MTAG_GENRE='%s'\n", g_shell_quote (taglib_tag_genre (tag)));
      break;

    case MTAG_MODE_SET:
      if (mtag_title)
        taglib_tag_set_title (tag, mtag_title);
      if (mtag_artist)
        taglib_tag_set_artist (tag, mtag_artist);
      if (mtag_album)
        taglib_tag_set_album (tag, mtag_album);
      if (mtag_year != -1)
        taglib_tag_set_year (tag, mtag_year);
      if (mtag_comment)
        taglib_tag_set_comment (tag, mtag_comment);
      if (mtag_track != -1)
        taglib_tag_set_track (tag, mtag_track);
      if (mtag_genre)
        taglib_tag_set_genre (tag, mtag_genre);
      taglib_file_save (file);
      break;
    }

  taglib_tag_free_strings ();
  taglib_file_free (file);

  return 0;
}
